package com.lance.core.launch.properties;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lancecong
 * @version 1.0
 * @ClassName CustomerProperties
 * @DESCRIPTION TODO
 * @create 2019-06-28 21:10
 **/
@Configuration
public class CustomerProperties implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext context) {
        ConfigurableEnvironment environment = context.getEnvironment();
        MutablePropertySources propertySources = environment.getPropertySources();
        propertySources.addLast(new MapPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, environment.getSystemProperties()));
        propertySources.addLast(new SystemEnvironmentPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, environment.getSystemEnvironment()));
        propertySources.addFirst(createPropertySources());
    }

    /**
     * 能自定义的加属性
     * @return
     */
    private PropertySource<?> createPropertySources() {
        Map<String, Object> source = new HashMap<>();
        source.put("name", "lancecong");
        PropertySource propertySource = new MapPropertySource("customerSource", source);
        return propertySource;
    }
}